package com.yaozheng.vocationaltraining.activity;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.ListView;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.MaintenanceEncyclopediaAdapter;
import com.yaozheng.vocationaltraining.iview.ILoadMoreView;
import com.yaozheng.vocationaltraining.iview.ITokenBaseView;
import com.yaozheng.vocationaltraining.manage.ListViewLoadMoreManage;
import com.yaozheng.vocationaltraining.service.MaintenanceEncyclopediaService;
import com.yaozheng.vocationaltraining.service.impl.MaintenanceEncyclopediaServiceImpl;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

/**
 * 维保百科
 * 
 * @author Administrator
 * 
 */
@EActivity(R.layout.activity_news_list)
public class MaintenanceEncyclopediaActivity extends BaseActivity implements
		SwipeRefreshLayout.OnRefreshListener, ILoadMoreView, ITokenBaseView {

	@ViewById
	ListView listView;

	/**
	 * 下拉刷新
	 */
	@ViewById
	SwipeRefreshLayout swipeRefreshLayout;

	ListViewLoadMoreManage listViewLoadMoreManage;

	@Bean(MaintenanceEncyclopediaServiceImpl.class)
	MaintenanceEncyclopediaService maintenanceEncyclopediaService;

	MaintenanceEncyclopediaAdapter maintenanceEncyclopediaAdapter;

	int lastId;

	@AfterViews
	public void initView() {
		setHeadTitle("维保百科");
		maintenanceEncyclopediaService.init(this);
		listViewLoadMoreManage = new ListViewLoadMoreManage(this, this,
				listView);
		swipeRefreshLayout.setOnRefreshListener(this);
//		swipeRefreshLayout.postDelayed(new Runnable() {
//			@Override
//			public void run() {
//				swipeRefreshLayout.setRefreshing(true);
//				onRefresh();
//			}
//		}, 500);

		onRefresh();
	}

	@Override
	public void loadMoreData() {
		if (maintenanceEncyclopediaAdapter != null) {
			int lastId = maintenanceEncyclopediaAdapter.getLastId();
			if (lastId > 0) {
				this.lastId = lastId;
				loadData();
			}
		}
	}

	@Override
	public void onRefresh() {
		lastId = 0;
		loadData();

	}

	public void loadData() {
		maintenanceEncyclopediaService.list(lastId);
	}

	@UiThread
	@Override
	public void responseSuccess(JSONObject jsonObject) {
		try {
			cancelLoadDataProgressDialog();
			JSONArray dataList = TypeUtils.getJsonArray(jsonObject, "data");
			if (dataList != null) {
				if (maintenanceEncyclopediaAdapter == null) {
					maintenanceEncyclopediaAdapter = new MaintenanceEncyclopediaAdapter(
							this, dataList);
					listView.setAdapter(maintenanceEncyclopediaAdapter);

				} else {
					if (lastId == 0) {
						maintenanceEncyclopediaAdapter.getDataList().clear();
					}
					maintenanceEncyclopediaAdapter.getDataList().addAll(
							dataList);
					maintenanceEncyclopediaAdapter.notifyDataSetChanged();
				}
			}
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
		}
	}

	@Override
	public void cancelLoadDataProgressDialog() {
		swipeRefreshLayout.setRefreshing(false);
		listViewLoadMoreManage.loadMoreComplete();
	}

	@UiThread
	@Override
	public void responseError(String error) {
		cancelLoadDataProgressDialog();
		alertMessage(error);
	}

}
